﻿using DuckClient.DuckServiceReference;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DuckEntities;
using DuckClient.Helpers;

namespace DuckClient
{
    public class DuckViewModel : ViewModelBase
    {
        private DuckTestingServiceClient proxy;

        public DuckViewModel(DuckTestingServiceClient proxy)
        {
            this.proxy = proxy;

            this.proxy.GetAllRaftsCompleted += proxy_GetAllRaftsCompleted;
            this.proxy.GetAllRaftsAsync();

            this.AddNewDuck = new RelayCommand(AddANewDuck);
            this.DeleteDuck = new RelayCommand(DeleteSelectedDuck);
            this.UpdateDuck = new RelayCommand(UpdateSelectedDuck);
            this.AddNewRaft = new RelayCommand(AddANewRaft);
            this.DeleteRaft = new RelayCommand(DeleteSelectedRaft);
            this.UpdateRaft = new RelayCommand(UpdateSelectedRaft);
            this.AddDuckToRaft = new RelayCommand(AddADuckToRaft);
            this.RemoveDuckFromRaft = new RelayCommand(DeleteADuckFromRaft);

            this.SelectOtherRaft = new RelayCommand(OpenRaftWindow);
            this.SelectOtherDuck = new RelayCommand(OpenDuckWindow);
        }

        public RelayCommand SelectOtherRaft { get; set; }

        private void OpenRaftWindow()
        {
            if (RaftsResult.Count > 0)
            {
                RaftPage rp = new RaftPage(RaftsResult, this);
            }
            else
            {
                return;
            }
        }

        private string sizeRaftList;

        public string SizeRaftList
        {
            get { return sizeRaftList; }
            set
            {
                if (sizeRaftList != value)
                {
                    sizeRaftList = value;
                    RaisePropertyChanged("SizeRaftList");
                }
            }
        }

        public RelayCommand SelectOtherDuck { get; set; }

        private void OpenDuckWindow()
        {
            if (DucksResult.Count > 0)
            {
                DuckPage dp = new DuckPage(DucksResult, this);
            }
            else
                return;
        }

        private string sizeDuckList;

        public string SizeDuckList
        {
            get { return sizeDuckList; }
            set
            {
                if (sizeDuckList != value)
                {
                    sizeDuckList = value;
                    RaisePropertyChanged("SizeDuckList");
                }
            }
        }
        
        void proxy_GetAllRaftsCompleted(object sender, GetAllRaftsCompletedEventArgs e)
        {            
            this.RaftsResult = e.Result;
            //ReturnDucks();
        }        

        private void proxy_GetAllDucksCompleted(object sender, GetAllDucksCompletedEventArgs e)
        {
            this.DucksResult = e.Result;
        }

        private List<DUCK> result;

        public List<DUCK> DucksResult
        {
            get { return result; }
            set
            {
                if (result != value)
                {
                    result = value;
                    SizeDuckList = "" + value.Count;
                    RaisePropertyChanged("DucksResult");
                }
            }
        }

        private DUCK selectedDuck;

        public DUCK SelectedDuck
        {
            get { return selectedDuck; }
            set
            {
                if (selectedDuck != value)
                {
                    selectedDuck = value;
                    NewDuckName = value.Name;
                    NewDuckColor = value.Color;
                    NewDuckAbout = value.About;
                    RaisePropertyChanged("SelectedDuck");
                }
            }
        }

        private string selectedDuckName;

        public string SelectedDuckName
        {
            get { return selectedDuckName; }
            set
            {
                if (selectedDuckName != value)
                {
                    selectedDuckName = value;
                    DUCK duck = new DUCK();

                    foreach (DUCK dck in DucksResult)
                    {
                        if (dck.Name == selectedDuckName)
                        {
                            SelectedDuck = dck;
                        }
                    }
                    RaisePropertyChanged("SelectedDuckName");
                }
            }
        }

        private List<RAFT> raftsResult;

        public List<RAFT> RaftsResult
        {
            get { return raftsResult; }
            set
            {
                if (raftsResult != value)
                {
                    raftsResult = value;
                    SizeRaftList = "" + value.Count;
                    RaisePropertyChanged("RaftsResult");
                }
            }
        }

        private RAFT selectedRaft;

        public RAFT SelectedRaft
        {
            get { return selectedRaft; }
            set
            {
                if (selectedRaft != value)
                {
                    selectedRaft = value;
                    ReturnDucks();
                    RaisePropertyChanged("SelectedRaft");
                }
            }
        }

        private string selectedRaftName;

        public string SelectedRaftName
        {
            get { return selectedRaftName; }
            set
            {
                if (selectedRaftName != value)
                {
                    selectedRaftName = value;
                    RAFT raft = new RAFT();

                    foreach (RAFT rft in RaftsResult)
                    {
                        if (rft.Name == selectedRaftName)
                        {
                            SelectedRaft = rft;
                        }
                    }
                    RaisePropertyChanged("SelectedRaftName");
                }
            }
        }

        private bool isChecked;

        public bool IsCheckedChbx
        {
            get { return isChecked; }
            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    ReturnDucks();
                    RaisePropertyChanged("IsCheckedChbx");
                }
            }
        }

        private void ReturnDucks()
        {
            DucksResult = new List<DUCK>();
            SelectedDuck = new DUCK(); ;
            //if (IsCheckedChbx)
            //{
            //    this.proxy.GetAllDucksRaftCompleted += proxy_GetAllDucksRaftCompleted;
            //    this.proxy.GetAllDucksRaftAsync(SelectedRaft);
            //}
            //else
            //{
            //    this.proxy.GetAllDucksCompleted += proxy_GetAllDucksCompleted;
            //    this.proxy.GetAllDucksAsync();
            //}
            this.proxy.GetAllDucksRaftCompleted += proxy_GetAllDucksRaftCompleted;
            this.proxy.GetAllDucksRaftAsync(SelectedRaft);
        }

        private string newRaftName;

        public string NewRaftName
        {
            get { return newRaftName; }
            set
            {                
                if (newRaftName != value)
                {
                    newRaftName = value;
                    RaisePropertyChanged("NewRaftName");
                }
            }
        }

        private string newRaftDescription;

        public string NewRaftDescription
        {
            get { return newRaftDescription; }
            set
            {
                if (newRaftDescription != value)
                {
                    newRaftDescription = value;
                    RaisePropertyChanged("NewRaftDescription");
                }
            }
        }       
        
        
        public RelayCommand AddNewDuck
        {
            get;
            private set;
        }

        public RelayCommand DeleteDuck
        {
            get;
            private set;
        }

        public RelayCommand UpdateDuck
        {
            get;
            private set;
        }

        public RelayCommand AddNewRaft
        {
            get;
            private set;
        }

        public RelayCommand DeleteRaft { get; set; }

        public RelayCommand UpdateRaft { get; set; }

        public RelayCommand AddDuckToRaft { get; set; }

        public RelayCommand RemoveDuckFromRaft { get; set; }

        private string newDuckName;

        public string NewDuckName
        {
            get { return newDuckName; }
            set
            {
                if (newDuckName != value)
                {
                    newDuckName = value;
                    RaisePropertyChanged("NewDuckName");
                }
            }
        }

        private string newDuckColor;

        public string NewDuckColor
        {
            get { return newDuckColor; }
            set
            {
                if (newDuckColor != value)
                {
                    newDuckColor = value;
                    RaisePropertyChanged("NewDuckColor");
                }
            }
        }

        private string newDuckAbout;

        public string NewDuckAbout
        {
            get { return newDuckAbout; }
            set
            {
                if (newDuckAbout != value)
                {
                    newDuckAbout = value;
                    RaisePropertyChanged("NewDuckAbout");
                }
            }

        }

        private void AddANewDuck()
        {
            DUCK duck = new DUCK();
            duck.Name = NewDuckName;
            duck.Color = NewDuckColor;
            duck.About = NewDuckAbout;
            NewDuckName = "";
            NewDuckColor = "";
            NewDuckAbout = "";

            this.proxy.AddNewDuckCompleted += proxy_AddNewDuckCompleted;
            this.proxy.AddNewDuckAsync(duck);
        }

        void proxy_AddNewDuckCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.proxy.GetAllDucksCompleted += proxy_GetAllDucksCompleted;
            this.proxy.GetAllDucksAsync();
        }

        private void DeleteSelectedDuck()
        {
            this.proxy.DeleteDuckCompleted += proxy_DeleteDuckCompleted;
            this.proxy.DeleteDuckAsync(SelectedDuck);
        }

        void proxy_DeleteDuckCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {           
            CleanTextBoxes();
            ReturnDucks();
        }

        private void UpdateSelectedDuck()
        {
            if (!string.IsNullOrWhiteSpace(NewDuckName))
                SelectedDuck.Name = NewDuckName;
            if (!string.IsNullOrWhiteSpace(NewDuckColor))
                SelectedDuck.Color = NewDuckColor;
            if (!string.IsNullOrWhiteSpace(NewDuckAbout))
                SelectedDuck.About = NewDuckAbout;

            this.proxy.UpdateDuckCompleted += proxy_UpdateDuckCompleted;
            this.proxy.UpdateDuckAsync(SelectedDuck);
        }

        private void CleanTextBoxes()
        {
            NewDuckName = "";
            NewDuckColor = "";
            NewDuckAbout = "";
            NewRaftName = "";
            NewRaftDescription = "";
        }

        void proxy_UpdateDuckCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SelectedDuck = null;
            CleanTextBoxes();
            this.proxy.GetAllDucksCompleted += proxy_GetAllDucksCompleted;
            this.proxy.GetAllDucksAsync();
        }

        void proxy_GetAllDucksRaftCompleted(object sender, GetAllDucksRaftCompletedEventArgs e)
        {
            this.DucksResult = e.Result;
            this.SizeDuckList = "" + e.Result.Count;
        }

        private void AddANewRaft()
        {
            RAFT raft = new RAFT();
            raft.Name = NewRaftName;
            raft.Description = NewRaftDescription;
            NewRaftName = "";
            NewRaftDescription = "";

            this.proxy.AddNewRaftCompleted += proxy_AddNewRaftCompleted;
            this.proxy.AddNewRaftAsync(raft);
        }

        void proxy_AddNewRaftCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.proxy.GetAllRaftsCompleted += proxy_GetAllRaftsCompleted;
            this.proxy.GetAllRaftsAsync();
        }

        private void DeleteSelectedRaft()
        {
            this.proxy.DeleteRaftCompleted += proxy_DeleteRaftCompleted;
            this.proxy.DeleteRaftAsync(SelectedRaft);
        }

        void proxy_DeleteRaftCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SelectedRaft = null;
            this.proxy.GetAllRaftsCompleted += proxy_GetAllRaftsCompleted;
            this.proxy.GetAllRaftsAsync();
        }

        private void UpdateSelectedRaft()
        {
            if (!string.IsNullOrWhiteSpace(NewRaftName))
                SelectedRaft.Name = NewRaftName;
            if (!string.IsNullOrWhiteSpace(NewRaftDescription))
                SelectedRaft.Description = NewRaftDescription;

            this.proxy.UpdateRaftCompleted += proxy_UpdateRaftCompleted;
            this.proxy.UpdateRaftAsync(SelectedRaft);
        }

        void proxy_UpdateRaftCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SelectedRaft = null;
            CleanTextBoxes();
            this.proxy.GetAllRaftsCompleted += proxy_GetAllRaftsCompleted;
            this.proxy.GetAllRaftsAsync();
        }

        private void AddADuckToRaft()
        {
            DUCKRAFT duckraft = new DUCKRAFT();
            duckraft.GroupID = SelectedRaft.GroupID;
            duckraft.DuckId = SelectedDuck.DuckID;
            this.proxy.AddDuckToRaftCompleted += proxy_AddDuckToRaftCompleted;
            this.proxy.AddDuckToRaftAsync(duckraft);
        }

        void proxy_AddDuckToRaftCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SelectedRaft = null;
            this.proxy.GetAllRaftsCompleted += proxy_GetAllRaftsCompleted;
            this.proxy.GetAllRaftsAsync();
        }

        private void DeleteADuckFromRaft()
        {
            this.proxy.DeleteDuckFromRaftCompleted += proxy_DeleteDuckFromRaftCompleted;
            this.proxy.DeleteDuckFromRaftAsync(SelectedDuck, SelectedRaft);
        }

        void proxy_DeleteDuckFromRaftCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SelectedRaft = null;
            this.proxy.GetAllRaftsCompleted += proxy_GetAllRaftsCompleted;
            this.proxy.GetAllRaftsAsync();
        }
    }
}
